//
//  ViewController.m
//  HBPhotoPicker
//
//  Created by hibo on 2019/5/20.
//  Copyright © 2019 hibo. All rights reserved.
//

#import "ViewController.h"
#import "HBPhotoManager.h"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView *collectionview;
@property (nonatomic,strong) NSArray *assetsList;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, HBNAVHEIGHT-44, HBScreenWidth, 44)];
    label.text = @"图片选择器";
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HBNAVHEIGHT, HBScreenWidth, HBScreenHeight-HBNAVHEIGHT) collectionViewLayout:layout];
    _collectionview.dataSource = self;
    _collectionview.delegate = self;
    _collectionview.backgroundColor = [UIColor whiteColor];
    _collectionview.scrollEnabled = YES;
    _collectionview.alwaysBounceVertical = YES;
    [_collectionview registerClass:[ViewControllerCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collectionview];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.assetsList.count+1;
}
- (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    ViewControllerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.row<self.assetsList.count) {
        PHAsset *asset = self.assetsList[indexPath.row];
        cell.asset = asset;
    }else{
        cell.addBtnString = @"hb_add_btn";
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row<self.assetsList.count) {
//        PHAsset *asset = self.assetsList[indexPath.row];
    }else{
        __weak typeof(self) weakSelf = self;
        //显示图片选择器
        [HBPhotoManager showPhotoControllerType:HBPhotoSelectTypeImage maxCount:9 result:^(NSArray * _Nonnull assets) {
            weakSelf.assetsList = assets;
            [weakSelf.collectionview reloadData];
        }];
    }
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((HBScreenWidth-25)/4.0,(HBScreenWidth-25)/4.0);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 5, 0, 5);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;//如果一组中有多行item,设置两个列的间距
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;//设置两个item的行间距
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(0, HBBarHEIGHT);//设置尾视图大小
}





@end


@implementation ViewControllerCell{
    UIImageView *imageview;
    CALayer *imageLayer;
    UILabel *timeLabel;//视频时间长度
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        imageview.contentMode = UIViewContentModeScaleAspectFill;
        imageview.clipsToBounds = YES;
        [self.contentView addSubview:imageview];
        
        imageLayer = [[CALayer alloc] init];
        imageLayer.frame = imageview.frame;
        imageLayer.hidden = YES;
        imageLayer.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2].CGColor;
        [imageview.layer addSublayer:imageLayer];
        
        //视频图片
        timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height-20, frame.size.width-5, 20)];
        timeLabel.textAlignment = NSTextAlignmentRight;
        timeLabel.textColor = [UIColor whiteColor];
        timeLabel.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview:timeLabel];
    }
    return self;
}
//获取图片显示
-(void)setAsset:(PHAsset *)asset{
    _asset = asset;
    imageview.image = nil;
    [HBPhotoManager getPhotoInfoWithAsset:asset targetSize:CGSizeMake(self.frame.size.width*HBScreenScale, self.frame.size.width*HBScreenScale) result:^(UIImage * _Nonnull image, PHAssetMediaType mediaType) {
        self->imageview.image = image;
        //视频
        NSString *time = @"";
        if (asset.duration<=60) {
            time = [NSString stringWithFormat:@"0:%d",(int)asset.duration];
        }else if(asset.duration<3600){
            time = [NSString stringWithFormat:@"%d:%d",(int)asset.duration/60,(int)asset.duration%60];
        }else{
            time = [NSString stringWithFormat:@"%d:%d:%d",(int)asset.duration/3600,(int)asset.duration%60/60,(int)asset.duration%3600];
        }
        self->timeLabel.text = mediaType == PHAssetMediaTypeVideo?time:@"";
    }];
}
-(void)setAddBtnString:(NSString *)addBtnString{
    imageview.image = [UIImage imageNamed:addBtnString];
}

@end
